﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PlateNumber {
    public enum FormMode {
        New = 1,
        Edit = 2,
        Test = 4,
        Formal = 8
    }

    public partial class DataPropertyForm : Form {

        public int Mode { get; set; }

        public string VIN {
            get {
                return txtVIN.Text.Trim();
            }
            set {
                txtVIN.Text = value;
            }
        }

        public string CardType {
            get {
                switch (ddlCardType.SelectedIndex) {
                    case 0:
                        return "A";
                    case 1:
                        return "B";
                    case 2:
                        return "C";
                }
                return String.Empty;
            }
            set {
                switch (value.ToUpper()) {
                    case "A":
                        ddlCardType.SelectedIndex = 0;
                        break;
                    case "B":
                        ddlCardType.SelectedIndex = 1;
                        break;
                    case "C":
                        ddlCardType.SelectedIndex = 2;
                        break;
                }
            }
        }

        public string CardNo {
            get {
                return txtCardNo.Text.Trim();
            }
            set {
                txtCardNo.Text = value;
            }
        }

        public string PlateNumber {
            get {
                return txtPlateNumber.Text.Trim();
            }
            set {
                txtPlateNumber.Text = value;
            }
        }

        public DateTime SubmitDate {
            get {
                return dtpSubmitDate.Checked ? dtpSubmitDate.Value : DateTime.MinValue;
            }
            set {
                if (value != DateTime.MinValue.Date) {
                    dtpSubmitDate.Value = value;
                }
                else {
                    dtpSubmitDate.Value = DateTime.Now;
                    dtpSubmitDate.Checked = false;
                }
            }
        }

        public int State {
            get {
                switch (ddlState.SelectedIndex) {
                    case 0:
                        return 1;
                    case 1:
                        return 0;
                    case 2:
                        return -1;
                    case 3:
                        return -2;
                    case 4:
                        return -3;
                }
                return Int32.MaxValue;
            }
            set {
                switch (value) {
                    case 1:
                        ddlState.SelectedIndex = 0;
                        break;
                    case 0:
                        ddlState.SelectedIndex = 1;
                        break;
                    case -1:
                        ddlState.SelectedIndex = 2;
                        break;
                    case -2:
                        ddlState.SelectedIndex = 3;
                        break;
                    case -3:
                        ddlState.SelectedIndex = 4;
                        break;
                }
            }
        }

        public bool Invalid {
            get {
                return rbtnInvalidTrue.Checked;
            }
            set {
                rbtnInvalidTrue.Checked = value;
                rbtnInvalidFalse.Checked = !value;
            }
        }

        public DataPropertyForm() {
            InitializeComponent();
        }

        private void DataPropertyForm_Load(object sender, EventArgs e) {
            if ((Mode & (int)FormMode.New) > 0) {
                // 新建
                ddlCardType.SelectedIndex = 0;
                rbtnInvalidFalse.Checked = true;
                dtpSubmitDate.Checked = false;
                ddlState.SelectedIndex = 1;
            }

            if ((Mode & (int)FormMode.Test) > 0) {
                // 测试
                ddlCardType.SelectedIndex = 2;
                ddlCardType.Enabled = false;
                txtCardNo.Enabled = false;
                txtVIN.Enabled = false;
                btnVIN.Enabled = false;

                Text = "探测数据 属性";
            }
            else {
                // 正式
                Text = "正式数据 属性";
            }

            // 新建测试
            if ((Mode & (int)FormMode.New) > 0 &&
                (Mode & (int)FormMode.Test) > 0) {
                txtVIN.Text = VINGenerator.Generate();
            }
        }

        private void btnOK_Click(object sender, EventArgs e) {

            if ((Mode & (int)FormMode.Formal) > 0) {
                // 正式
                if (txtVIN.Text.Trim() == String.Empty) {
                    MessageBox.Show(this, "请填写车辆标识代号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtVIN.Focus();
                    return;
                }

                if (txtPlateNumber.Text.Trim() == String.Empty) {
                    MessageBox.Show(this, "请填写预选车牌号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPlateNumber.Focus();
                    return;
                }

                if (txtCardNo.Text.Trim() == String.Empty) {
                    MessageBox.Show(this, "请填写证件号码。", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtCardNo.Focus();
                    return;
                }
            }
            else {
                // 测试
                if (txtPlateNumber.Text.Trim() == String.Empty) {
                    MessageBox.Show(this, "请填写预选车牌号。", Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtPlateNumber.Focus();
                    return;
                }
            }

            DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
        }

        private void button1_Click(object sender, EventArgs e) {
            txtVIN.Text = VINGenerator.Generate();
        }
    }
}
